package com.lagou.edu.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Configuration
@ComponentScan({"com.lagou.edu"})
public class SpringConfig {

    @Bean
    public DataSource newDataSource() throws IOException {
        Properties properties;
        try (InputStream in = SpringConfig.class.getClassLoader().getResourceAsStream("jdbc.properties")) {
            properties = new Properties();
            properties.load(in);
        }

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(properties.getProperty("jdbc.driverClass"));
        dataSource.setUrl(properties.getProperty("jdbc.url"));
        dataSource.setUsername(properties.getProperty("jdbc.username"));
        dataSource.setPassword(properties.getProperty("jdbc.password"));
        return dataSource;
    }

}
